package com.greatchn.single_login.handler;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.ContentType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @AccDeniedHandler: 访问权限异常处理
 * @author: ZBoHang
 * @time: 2023/2/9 11:17
 */
@Component
public class AccDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setContentType(ContentType.JSON.getValue());
        response.setCharacterEncoding(CharsetUtil.UTF_8);
        response.getWriter().write("访问异常!");
    }
}
